#  -*- coding:utf-8 -*- 
"""
@ author: 罗金盛
@ time: 2024/3/6 
@ file: 14.多线程上锁.py

"""
import threading

Lock = threading.Lock()

num = 0

def add():
    global num
    for i in range(100000):
        Lock.acquire()  # 上锁
        num+=1
        Lock.release()  # 释放

def sub():
    global num
    for i in range(100000):
        Lock.acquire()
        num-=1
        Lock.release()

t1= threading.Thread(target=add)
t2= threading.Thread(target=sub)

t1.start()
t2.start()

t1.join()
t2.join()

print(num)
print('主线程已经结束！')



